﻿@using NB.MGJ.Model
@{
    Layout = null;
    L.EntityModel.AntiFake_QueryReply QueryReply = ViewBag.QueryReply_Page as L.EntityModel.AntiFake_QueryReply;
}
@*假码查询回复设置*@

<script src="~/Scripts/WebUploader/webuploader.min.js"></script>

<script>
    function showPanel(obj) {
        $(obj).parent().parent().nextAll('.panel-collapse').toggle();
    }
</script>
<style>
    #ReplyPseudoCodepage #pick input[type="file"] {
        opacity: 0;
    }

    #ReplyPseudoCodepage .row .form-control p {
        margin: 0;
    }

    #ReplyPseudoCodepage .row {
        margin: 30px;
    }
</style>

<div class="mtlr15">
    <form id="ReplyPseudoCodepage" method="post">
        <div class="row">
            <label class="col-sm-2 control-label text-right">选择语音文件：</label>
            <div class="col-sm-6" style="display:flex;">
                <label id="pick" class="btn btn-success">文件</label>
                <div class="form-control"></div>
            </div>
        </div>
        <div class="row">
            <label class="col-sm-2 control-label text-right"><span class="red-700">*</span>手机短信内容：</label>
            <div class="col-sm-6">
                <input id="MessageContent" class="form-control" type="text" name="MessageContent" placeholder="@QueryReply.MessageContent" />
            </div>
        </div>
        <div class="row">
            <label class="col-sm-2 control-label text-right"><span class="red-700">*</span>网站内容：</label>
            <div class="col-sm-6">
                <input id="WebContent" class="form-control" type="text" name="WebContent" placeholder="@QueryReply.WebContent" />
            </div>
        </div>
    </form>
    <div class="row">
        <div class="new-row col-sm-8" style="justify-content:center">
            <button id="save" class="btn btn-success ">
                <i class="glyphicon glyphicon-upload"></i>
                文件保存
            </button>
        </div>
    </div>
</div>

<script>
    var fileVal;
    $(function () {
        var uploader = WebUploader.create({
            // swf文件路径
            //swf: BASE_URL + '/js/Uploader.swf',
            // 文件接收服务端。
            server: '../AntiFake/FileUpload?MessageContent=' + $('#MessageContent').val() + '&WebContent=' + $('#WebContent').val(),
            // 选择文件的按钮。可选。
            // 内部根据当前运行是创建，可能是input元素，也可能是flash.
            pick: {
                id: '#pick',
                multiple: false,
            },
            accept: {
                title: '文本',
                extensions: 'txt',
                mimeTypes: '*.txt'
            },
            // 不压缩image, 默认如果是jpeg，文件上传前会压缩一把再上传！
            resize: false
        });

        // 当有文件被添加进队列的时候
        uploader.on('fileQueued', function (file) {
            fileVal = file;
            $('#pick').next().children().remove();
            $('#pick').next().append('<div id="' + file.id + '" >' +
                '<span class="info">' + file.name + '</span>' +
                '<p class="state">等待上传...</p>' +
            '</div>');
            //$('#pick').attr('disabled', 'disabled');
            //$('#pick input[type="file"]').attr('disabled', 'disabled');
        });
        // 文件上传过程中创建进度条实时显示。
        uploader.on('uploadProgress', function (file, percentage) {
            var $li = $('#' + file.id),
                $percent = $li.find('.progress .progress-bar');
            // 避免重复创建
            if (!$percent.length) {
                $percent = $('<div class="progress progress-striped active" style="margin:0">' +
                  '<div class="progress-bar" role="progressbar" style="width: 0%">' +
                  '</div>' +
                '</div>').appendTo($li).find('.progress-bar');
            }
            $li.find('p.state').text('上传中');
            $percent.css('width', percentage * 100 + '%');
        });
        uploader.on('uploadSuccess', function (file, r) {
            $('#' + file.id).find('p.state').text('已上传');
            if ($('#' + file.id).find('p.state').text('已上传')) {
                //$.L.msgSuccess("操作成功！");
            }
        });

        uploader.on('uploadError', function (file, r) {
            $('#' + file.id).find('p.state').text('上传失败');
            if ($('#' + file.id).find('p.state').text('上传失败')) {
                $.L.msgSuccess("操作失败！");
            }
        });

        uploader.on('uploadComplete', function (file) {
            $('#' + file.id).find('.progress').fadeOut();
        });

        $('#save').click(function () {
           
            var bootstrapValidator = $("#ReplyPseudoCodepage").data('bootstrapValidator').validate();
            if (bootstrapValidator.isValid()) {
                
                var mec = $('#MessageContent').val();
                var wcs = $('#WebContent').val();
                if ($(".info").text() != "") {
                    uploader.options.formData = { "MessageContent": mec, "WebContent": wcs };
                    uploader.upload();
                    $.L.msgSuccess("操作成功！");
                }
                else {
                    $.L.ajaxHandle({
                        url: ACTION_URL("FileUpload", "AntiFake"),
                        data: {
                            MessageContent: mec,
                            WebContent: wcs
                        },
                        success: function (response) {
                            console.log(response);
                            $.L.msgSuccess("操作成功！");
                        }
                    });
                
                }
            }
            else {
                $.L.msgError("请检查数据格式！");
            };
        })
    });

    $('#ReplyPseudoCodepage').bootstrapValidator({
        fields: {
            MessageContent: {
                validators: {
                    notEmpty: {
                        message: '手机短信内容不能为空'
                    },
                }
            },
            WebContent: {
                validators: {
                    notEmpty: {
                        message: '网站内容不能为空'
                    },
                }
            },
        }
    });
</script>